package com.danco.training.zaneuskaya.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.repository.CityDao;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class CityDaoImpl extends AbstractHibernateDao<City, Integer> implements CityDao{

	private static final String CITY_NAME = "cityName";
	private static final String CITY_COUNTRY = "country";

	@Override
	public City read(Integer PK) throws HibernateException {
		return super.read(City.class, PK);
	}

	@Override
	public List<City> readAll() throws HibernateException {
		return super.readAll(City.class, CITY_NAME);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<City> findCitiesByCountry(Country country) throws HibernateException{
		List<City> cities = new ArrayList<City>();
		Criteria criteria = getSession().createCriteria(City.class);
		criteria.add(Restrictions.eq(CITY_COUNTRY, country));
		cities = modifyCriteria(criteria, CITY_NAME).list();
		return cities;
	}

	

}
